package com.lineage.data.item_etcitem.brave;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.skill.L1BuffUtil;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillBrave;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ForeverStatusBrave extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(ForeverStatusBrave.class);
  
  private static final int _int8 = 68;
  
  private static final int _int7 = 64;
  
  private static final int _int6 = 32;
  
  private static final int _int5 = 16;
  
  private static final int _int4 = 8;
  
  private static final int _int3 = 4;
  
  private static final int _int2 = 2;
  
  private static final int _int1 = 1;
  
  private int _time = 300;
  
  private int _gfxid = 0;
  
  private int _need = 0;
  
  private int _count = 0;
  
  private boolean _isCrown;
  
  private boolean _isKnight;
  
  private boolean _isElf;
  
  private boolean _isWizard;
  
  private boolean _isDarkelf;
  
  private boolean _isDragonKnight;
  
  private boolean _isIllusionist;
  
  private boolean _isWarrior;
  
  public static ItemExecutor get() {
    return new ForeverStatusBrave();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    L1Item needitem = ItemTable.get().getTemplate(this._need);
    if (item == null)
      return; 
    if (pc == null)
      return; 
    if (!pc.getInventory().checkItem(this._need, this._count)) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(String.valueOf(needitem.getName())) + "不足，需要" + this._count + "个才能使用"));
      return;
    } 
    pc.getInventory().consumeItem(this._need, this._count);
    if (L1BuffUtil.stopPotion(pc))
      if (check(pc)) {
        if (pc.hasSkillEffect(186)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1413));
          return;
        } 
        if (pc.hasSkillEffect(1017)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1413));
          return;
        } 
        L1BuffUtil.cancelAbsoluteBarrier(pc);
        if (this._gfxid > 0)
          pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), this._gfxid)); 
        pc.sendPackets((ServerBasePacket)new S_SkillBrave(pc.getId(), 1, this._time));
        pc.broadcastPacketAll((ServerBasePacket)new S_SkillBrave(pc.getId(), 1, 0));
        pc.setSkillEffect(1000, this._time * 1000);
        pc.setBraveSpeed(1);
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("使用名誉货币，扣除【金币】:" + this._count));
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      }  
  }
  
  private boolean check(L1PcInstance pc) {
    try {
      if (pc.isCrown() && this._isCrown)
        return true; 
      if (pc.isKnight() && this._isKnight)
        return true; 
      if (pc.isElf() && this._isElf)
        return true; 
      if (pc.isWizard() && this._isWizard)
        return true; 
      if (pc.isDarkelf() && this._isDarkelf)
        return true; 
      if (pc.isDragonKnight() && this._isDragonKnight)
        return true; 
      if (pc.isIllusionist() && this._isIllusionist)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
  
  private void set_use_type(int use_type) {
    try {
      if (use_type >= 68) {
        use_type -= 68;
        this._isWarrior = true;
      } 
      if (use_type >= 64) {
        use_type -= 64;
        this._isIllusionist = true;
      } 
      if (use_type >= 32) {
        use_type -= 32;
        this._isDragonKnight = true;
      } 
      if (use_type >= 16) {
        use_type -= 16;
        this._isDarkelf = true;
      } 
      if (use_type >= 8) {
        use_type -= 8;
        this._isWizard = true;
      } 
      if (use_type >= 4) {
        use_type -= 4;
        this._isElf = true;
      } 
      if (use_type >= 2) {
        use_type -= 2;
        this._isKnight = true;
      } 
      if (use_type >= 1) {
        use_type--;
        this._isCrown = true;
      } 
      if (use_type > 0)
        _log.error("StatusBrave 可执行职业设定错误:余数大于0"); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._time = Integer.parseInt(set[1]);
      if (this._time <= 0) {
        _log.error("StatusBrave 设置错误:技能效果时间小于等于0! 使用预设300秒");
        this._time = 300;
      } 
    } catch (Exception exception) {}
    try {
      this._gfxid = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      int user_type = Integer.parseInt(set[3]);
      set_use_type(user_type);
    } catch (Exception exception) {}
    try {
      this._need = Integer.parseInt(set[4]);
    } catch (Exception exception) {}
    try {
      this._count = Integer.parseInt(set[5]);
    } catch (Exception exception) {}
  }
}
